package com.example.controller;

import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.example.config.Result;
import com.example.service.UserService;
import com.example.pojo.MsslyyLabelBook;
import com.example.service.MsslyyLabelBookService;
import com.example.pojo.User;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/msslyyLabelBook")
@Api(tags = "分配标签表")
public class MsslyyLabelBookController {
    @Resource
    private MsslyyLabelBookService msslyyLabelBookService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private UserService userService;

    public User getUser() {
        String token = request.getHeader("token");
        String username = JWT.decode(token).getAudience().get(0);
        return userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, username));
    }

    @ApiOperation(value = "新增分配标签",notes = "")
    @PostMapping
    public Result<?> save(@RequestBody MsslyyLabelBook msslyyLabelBook) {
        msslyyLabelBookService.save(msslyyLabelBook);
        return Result.success();
    }

    @ApiOperation(value = "更新分配标签",notes = "")
    @PutMapping
    public Result<?> update(@RequestBody MsslyyLabelBook msslyyLabelBook) {
        msslyyLabelBookService.updateById(msslyyLabelBook);
        return Result.success();
    }

    @ApiOperation(value = "删除分配标签",notes = "")
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        msslyyLabelBookService.removeById(id);
        return Result.success();
    }

    @ApiOperation(value = "根据id查询",notes = "")
    @GetMapping("/{id}")
    public Result<?> findById(@PathVariable Long id) {
        return Result.success(msslyyLabelBookService.getById(id));
    }

    @ApiOperation(value = "查询全部",notes = "")
    @GetMapping
    public Result<?> findAll() {
        List<MsslyyLabelBook> list = msslyyLabelBookService.list();
        return Result.success(list);
    }

    @ApiOperation(value = "分页",notes = "")
    @GetMapping("/page")
    public Result<?> findPage(@RequestParam(required = false, defaultValue = "") String name,
                                                @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<MsslyyLabelBook> query = Wrappers.<MsslyyLabelBook>lambdaQuery().orderByDesc(MsslyyLabelBook::getId);
        if (StrUtil.isNotBlank(name)) {
            query.like(MsslyyLabelBook::getBookId, name);
        }
        IPage<MsslyyLabelBook> page = msslyyLabelBookService.page(new Page<>(pageNum, pageSize), query);
        return Result.success(page);
    }

}
